package com.gitee.pifeng.monitoring.ui.business.web.controller;

import com.gitee.pifeng.monitoring.common.exception.NetException;
import com.gitee.pifeng.monitoring.ui.business.web.annotation.OperateLog;
import com.gitee.pifeng.monitoring.ui.business.web.entity.MonitorConfigUser;
import com.gitee.pifeng.monitoring.ui.business.web.service.IMonitorConfigService;
import com.gitee.pifeng.monitoring.ui.business.web.vo.LayUiAdminResultVo;
import com.gitee.pifeng.monitoring.ui.business.web.vo.MonitorConfigPageFormVo;
import com.gitee.pifeng.monitoring.ui.constant.OperateTypeConstants;
import com.gitee.pifeng.monitoring.ui.constant.UiModuleConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.hyperic.sigar.SigarException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 监控配置
 * </p>
 *
 * @author 皮锋
 * @custom.date 2020/11/3 12:57
 */
@Controller
@Slf4j
@RequestMapping("/monitor-config")
@Tag(name = "配置管理.监控配置")
public class MonitorConfigController {

    /**
     * 监控配置服务类
     */
    @Autowired
    private IMonitorConfigService monitorConfigService;

    /**
     * <p>
     * 访问监控配置页
     * </p>
     *
     * @return {@link ModelAndView} 监控配置页
     * @author 皮锋
     * @custom.date 2020/11/3 13:01
     */
    @Operation(summary = "访问监控配置页")
    @GetMapping("/config")
    public ModelAndView config() {
        ModelAndView mv = new ModelAndView("set/config");
        MonitorConfigPageFormVo monitorConfigPageForm = this.monitorConfigService.getMonitorConfigPageFormInfo();
        mv.addObject("monitorConfigPageForm", monitorConfigPageForm);
        return mv;
    }

    /**
     * <p>
     * 更新监控配置
     * </p>
     *
     * @param monitorConfigPageFormVo 监控配置页面表单对象
     * @return layUiAdmin响应对象：如果更新数据库成功，LayUiAdminResultVo.data="success"；<br>
     * 如果更新数据库成功，但是更新监控服务端配置失败，LayUiAdminResultVo.data="refreshFail"；<br>
     * 否则，LayUiAdminResultVo.data="fail"。
     * @throws NetException   自定义获取网络信息异常
     * @throws SigarException Sigar异常
     * @author 皮锋
     * @custom.date 2020/11/9 19:56
     */
    @Operation(summary = "更新监控配置")
    @ResponseBody
    @PutMapping("/update-monitor-config")
    @OperateLog(operModule = UiModuleConstants.CONFIG_MANAGE + "#监控配置", operType = OperateTypeConstants.UPDATE, operDesc = "更新监控配置")
    public LayUiAdminResultVo updateMonitorConfig(MonitorConfigPageFormVo monitorConfigPageFormVo) throws NetException, SigarException {
        log.info("=======修改数据=========="+monitorConfigPageFormVo);
        MonitorConfigUser monitorConfigUser =new MonitorConfigUser();
        monitorConfigUser.setConfigID(1L);
        List<MonitorConfigUser> monitorConfigUsers=this.monitorConfigService.selectmonitorConfigUser(monitorConfigUser);
        log.info("=======monitorConfigUsers=========="+monitorConfigUsers);
        log.info("=======monitorConfigUsers.size=========="+monitorConfigUsers.size());
            ArrayList<String> list=new ArrayList<>();
        if (monitorConfigUsers.size()!=0){
            for (int i = 0; i < monitorConfigUsers.size(); i++) {
                list.add(monitorConfigUsers.get(i).getEmail());
          }
        }else{
            monitorConfigPageFormVo.setAlarmMailEmills("");
        }
        log.info("=======list=========="+list);
        String a="";
        a=String.valueOf(list);
        a=a.replace("[","").replace("]","").replace(",",";");
        monitorConfigPageFormVo.setAlarmMailEmills(a);
        log.info("=======getAlarmMailEmills=========="+monitorConfigPageFormVo.getAlarmMailEmills());
        return this.monitorConfigService.update(monitorConfigPageFormVo);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/delete-monitor-user-record")
    @ResponseBody
    public Long deleteMonitorAlarmRecord(@RequestBody Long ConfigID) {
        log.info("=======返回数据=========="+ConfigID);
        return this.monitorConfigService.deleteconfigId(ConfigID);
    }

    @PostMapping("/insert-monitor-user")
    @ResponseBody
    public LayUiAdminResultVo addMonitorConfigUser(@RequestBody Long[] userID) {
        log.info("=======新增返回数据=========="+userID);
        log.info("=======新增返回数据长度=========="+userID.length);
        ArrayList<MonitorConfigUser> userList = new ArrayList<>();
        if (userID != null) {
            for (int i = 0; i < userID.length; i++) {
                // 根据用户id循环遍历插入
                MonitorConfigUser monitorConfigUser =new MonitorConfigUser();
                monitorConfigUser.setUserID(userID[i]);
                monitorConfigUser.setConfigID(1L);
                monitorConfigUser.setChecked(1);
                userList.add(monitorConfigUser);
            }
            log.info("=======userList=========="+userList);
            return LayUiAdminResultVo.ok(this.monitorConfigService.insertmonitorConfigUser(userList));
        }else {
            return LayUiAdminResultVo.fail("批量新增失败");
        }
    }

}
